﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web.Mvc;
using ScrumPilot.Domain.BPL;
using ScrumPilot.Domain.Models;
using ScrumPilot.Helpers;

namespace ScrumPilot.Controllers
{
    [Authorize]
    public class SprintController : BaseController
    {
        private UserStoriesServices _usm;
        protected UserStoriesServices UserStoriesSrv { get { return _usm ?? (_usm = new UserStoriesServices(this.TFS)); } }

        private SprintsServices _spt;
        protected SprintsServices SprintsSrv { get { return _spt ?? (_spt = new SprintsServices(this.TFS)); } }

        [HttpGet]
        public ActionResult SprintIndex()
        {
            return View(this.UserStoriesSrv.CurrentSprint);
        }

        [HttpGet, NoCache]
        public ActionResult UserStoriesSprint()
        {
            if (TFS == null)
                return RedirectToAction("logoff", "Account");
            var myList = this.UserStoriesSrv.GetUserStoriesInSprint(this.CurrentUser,this.TfsRootUrl);
            return Json(myList, JsonRequestBehavior.AllowGet);
        }

        [HttpGet, NoCache]
        public ActionResult SprintsDef()
        {
            var mySprints = this.SprintsSrv.GetSprints();
            return View(mySprints);
        }

        [HttpGet, NoCache]
        // ReSharper disable InconsistentNaming
        public PartialViewResult _UpdateSprintDef(int id)
        // ReSharper restore InconsistentNaming
        {
            var mySprint = this.SprintsSrv.GetSprint(id);
            return PartialView(mySprint);
        }

        [HttpPost]
        public EmptyResult UpdateSprintDef(Sprint model)
        {
            this.SprintsSrv.SetSprint(model);
            return null;
        }

        [HttpPost]
        public ActionResult DeleteSprintDef(int id)
        {
            throw new NotImplementedException();
            //return null;
        }

        [HttpGet, NoCache]
        // ReSharper disable InconsistentNaming
        public PartialViewResult _SprintFollowedProject(int id)
        // ReSharper restore InconsistentNaming
        {
            var mySprints = this.SprintsSrv.GetFollowedCurrentIteration(id);
            return PartialView(mySprints);
        }

        [HttpPost]
        public EmptyResult SetIterations(int sprintId, int[] selectedValues)
        {
            this.SprintsSrv.SetIterations(sprintId, selectedValues);
            return null;
        }
    }
}
